//
//  RecommendContentViewController.m
//  网络FM
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "RecommendContentViewController.h"
#import <AFNetworking.h>
#import "RecommendContentModel.h"
#import "RecommendContentTableViewCell.h"
#import "PlayerViewController.h"
@interface RecommendContentViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *bigArray;
@end

@implementation RecommendContentViewController
- (void)dealloc
{
    [_pagenum release];
    [_pid release];
    [_contentTitle release];
    [_bigArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
    }
    return self;
}
- (void)contentTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView release];
}
- (void)loadView
{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self recommendConnection];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationItem.title = self.contentTitle;
    // Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.bigArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"contentCell";
    RecommendContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[RecommendContentTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    RecommendContentModel *model = [self.bigArray objectAtIndex:indexPath.row];
    [cell.titleLable setText:model.compere];
    NSString *string = [NSString stringWithFormat:@"❤️ %@",model.collectNumShow];
    [cell.likeLable setText:string];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
#pragma mark -
#pragma mark 数据请求
- (void)recommendConnection
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://d.fm.ifeng.com/fm/read/fmd/ios/600/getProgramAudioList.html?appid=TRWLY&listorder=desc&pagenum=1&pid=%@&token=KhwbSbnG7FGcDnFn", self.pid];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic = [bigDic objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            RecommendContentModel *model = [[RecommendContentModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        [self contentTableView];
    } failure:
     ^(AFHTTPRequestOperation *operation, NSError *error) {
         
     }];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *player = [[PlayerViewController alloc] init];
    RecommendContentModel *model = [self.bigArray objectAtIndex:indexPath.row];
    player.urlForPlay = model.videoUrl;
    [self.navigationController pushViewController:player animated:YES];
    [player release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
